package com.example.harmans;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;

public class MainActivity extends Activity {

	private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);
 
        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));
 
        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
 
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );
 
        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
 
        getActionBar().setDisplayHomeAsUpEnabled(true); 
 
        // just styling option add shadow the right edge of the drawer
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
		drawerListView.setOnItemClickListener(new DrawerItemClickListener());

		addListenerButton();
	}

	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

	private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            String link = (String) ((TextView)view).getText();

            if(link.equals("Home")) {
        		Intent homeActivity = new Intent(MainActivity.this,MainActivity.class);
        		startActivity(homeActivity);
            } else if(link.equals("Find A Court")) {
        		Intent courtActivity = new Intent(MainActivity.this,CourtActivity.class);
		    	startActivity(courtActivity);
            } else if(link.equals("Court Fees")) {
        		Intent feesActivity = new Intent(MainActivity.this,FeesActivity.class);
		    	startActivity(feesActivity);
            } else if(link.equals("Book A Collection")) {
        		Intent collectionActivity = new Intent(MainActivity.this,CollectionActivity.class);
		    	startActivity(collectionActivity);
            } else if(link.equals("Interest Calculator")) {
        		Intent calcActivity = new Intent(MainActivity.this,CalculatorActivity.class);
		    	startActivity(calcActivity);
            } else if(link.equals("FAQ")) {
        		Intent faqActivity = new Intent(MainActivity.this,FaqActivity.class);
		    	startActivity(faqActivity);
            } else if(link.equals("Contact")) {
        		Intent contactActivity = new Intent(MainActivity.this,ContactActivity.class);
		    	startActivity(contactActivity);
            } else if(link.equals("About")) {
    			Intent aboutActivity = new Intent(MainActivity.this,AboutActivity.class);
		    	startActivity(aboutActivity);
            }
            drawerLayout.closeDrawer(drawerListView);
        }
    }

	public void addListenerButton() {
		
		RelativeLayout courtLayout = (RelativeLayout)findViewById(R.id.courtLayout);
		courtLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent courtActivity = new Intent(MainActivity.this,CourtActivity.class);
		    	startActivity(courtActivity);
			}
			
		});
		RelativeLayout feesLayout = (RelativeLayout)findViewById(R.id.feesLayout);
		feesLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent feesActivity = new Intent(MainActivity.this,FeesActivity.class);
		    	startActivity(feesActivity);
			}
			
		});
		RelativeLayout collectionLayout = (RelativeLayout)findViewById(R.id.collectionlayout);
		collectionLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent collectionActivity = new Intent(MainActivity.this,CollectionActivity.class);
		    	startActivity(collectionActivity);
			}
			
		});
		RelativeLayout calcLayout = (RelativeLayout)findViewById(R.id.midLayout);
		calcLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent calcActivity = new Intent(MainActivity.this,CalculatorActivity.class);
		    	startActivity(calcActivity);
			}
			
		});
		RelativeLayout faqLayout = (RelativeLayout)findViewById(R.id.faqLayout);
		faqLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent faqActivity = new Intent(MainActivity.this,FaqActivity.class);
		    	startActivity(faqActivity);
			}
			
		});
		RelativeLayout contactLayout = (RelativeLayout)findViewById(R.id.contactLayout);
		contactLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent contactActivity = new Intent(MainActivity.this,ContactActivity.class);
		    	startActivity(contactActivity);
			}
			
		});
		RelativeLayout aboutLayout = (RelativeLayout)findViewById(R.id.aboutLayout);
		aboutLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Intent aboutActivity = new Intent(MainActivity.this,AboutActivity.class);
				Intent aboutActivity = new Intent(MainActivity.this,AboutActivity.class);
		    	startActivity(aboutActivity);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.calculator, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			startActivity(new Intent(MainActivity.this,MainActivity.class)); 
			return true;
		}
	    if(item.getItemId() == R.id.icon_calculator) {
	    	Intent calcIntent = new Intent(MainActivity.this,CalculatorActivity.class);
	    	startActivity(calcIntent);
	    }
	    if(item.getItemId() == R.id.menu_court) {
	    	Intent courtIntent = new Intent(MainActivity.this,CourtActivity.class);
	    	startActivity(courtIntent);
	    	//overridePendingTransition(R.anim.slid_in, R.anim.slid_out);
	    }
	    if(item.getItemId() == R.id.menu_fees) {
	    	Intent feesIntent = new Intent(MainActivity.this,FeesActivity.class);
	    	startActivity(feesIntent);
	    }
	    if(item.getItemId() == R.id.menu_collection) {
	    	Intent collectionIntent = new Intent(MainActivity.this,CollectionActivity.class);
	    	startActivity(collectionIntent);
	    }
	    if(item.getItemId() == R.id.menu_faq) {
	    	Intent faqIntent = new Intent(MainActivity.this,FaqActivity.class);
	    	startActivity(faqIntent);
	    }
	    if(item.getItemId() == R.id.menu_contact) {
	    	Intent contactIntent = new Intent(MainActivity.this,ContactActivity.class);
	    	startActivity(contactIntent);
	    }
	    if(item.getItemId() == R.id.menu_about) {
	    	Intent aboutIntent = new Intent(MainActivity.this,AboutActivity.class);
	    	startActivity(aboutIntent);
	    }
	    return true;
	}

}
